package cn.keyingbo.nettylearning.message.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.EventExecutorGroup;
import org.apache.log4j.Logger;

/**
 * 客户端消息处理类
 */
public class MessageClientHandler extends ChannelInboundHandlerAdapter {
    private static Logger log = Logger.getLogger(MessageClientHandler.class.getClass());
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        /**
         * 先发送hello 再 bye
         */
        ByteBuf message = null;
        message = Unpooled.copiedBuffer(("hello" + System.getProperty("line.separator")).getBytes());
        ctx.writeAndFlush(message);

        message = Unpooled.copiedBuffer(("bye" + System.getProperty("line.separator")).getBytes());
        ctx.writeAndFlush(message);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info(" 连接断开");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //服务器返回消息
        String body = (String) msg;
        log.info("服务器返回消息：" + body);
    }
}
